package com.tencent.sr.iris.basic.saas.service.model.activity;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.tencent.sr.iris.basic.saas.common.enums.YesOrNoEnum;
import com.tencent.sr.iris.basic.saas.interfaces.response.activity.ActivityTaskPageResponse;
import com.tencent.sr.iris.user.core.service.interfaces.enums.UserLevelEnum;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.Objects;
import java.util.Optional;

@Data
@ApiModel(value = "活动明细导出信息")
@Slf4j
@ColumnWidth(15)
public class BaseActivityTaskExportInfo {

    @ExcelProperty(value = "序号", index = 0)
    protected int num;

    @ExcelProperty(value = "下级uid", index = 1)
    protected String uid;

    @ExcelProperty(value = "下级昵称", index = 2)
    protected String nickname;

    @ExcelProperty(value = "下级等级", index = 3)
    protected String userLevel;

    @ExcelProperty(value = "下级unionId", index = 4)
    protected String unionId;

    @ExcelProperty(value = "下级所属区域", index = 5)
    protected String area;

    @ExcelProperty(value = "下级是否特邀", index = 6)
    private String uidSpecialInvite;

    @ExcelProperty(value = "下级特邀时间", index = 7)
    private String specialInviteTime;

    @ExcelProperty(value = "成为见习会员时间", index = 8)
    protected String traineeTime;

    @ExcelProperty(value = "上级uid", index = 9)
    protected String pid;

    @ExcelProperty(value = "上级昵称", index = 10)
    protected String pnickname;

    @ExcelProperty(value = "上级等级", index = 11)
    protected String puserLevel;

    @ExcelProperty(value = "上级是否特邀用户", index = 12)
    private String pidSpecialInvite;

    @ExcelProperty(value = "上级所属区域", index = 13)
    private String superiorArea;

    @ExcelProperty(value = "上级特邀时间", index = 14)
    private String superiorSpecialInviteTime;

    @ExcelProperty(value = "导师uid", index = 15)
    protected String tuid;

    @ExcelProperty(value = "导师昵称", index = 16)
    protected String tnickname;

    public void readFromSearchResponseData(ActivityTaskPageResponse response, Map<Long, String> areaMap) {

        this.setUid(response.getUid());
        this.setNickname(response.getNickname());
        this.setUnionId(response.getUnionId());
        this.setPid(response.getPid());
        this.setPnickname(response.getPnickname());
        this.setTuid(response.getTuid());
        this.setTnickname(response.getTnickname());

        String userLevel = Optional.ofNullable(UserLevelEnum.getByValue(response.getUserLevel()))
                .map(UserLevelEnum::getDesc).orElse(StrUtil.EMPTY);
        this.setUserLevel(userLevel);

        this.setArea(areaMap.getOrDefault(response.getAreaId(), StrUtil.EMPTY));
        this.setUidSpecialInvite(YesOrNoEnum.getByValue(response.getUidSpecialInvite()));

        if (Objects.equals(response.getUidSpecialInvite(), YesOrNoEnum.YES.getValue())) {
            String specialInviteTime = Optional.ofNullable(response.getSpecialInviteTime())
                    .map(i -> DateUtil.formatDateTime(DateUtil.date(i))).orElse(StrUtil.EMPTY);
            this.setSpecialInviteTime(specialInviteTime);
        } else {
            this.setSpecialInviteTime(StrUtil.DASHED);
        }

        String traineeTime = Optional.ofNullable(response.getTraineeTime())
                .map(i -> DateUtil.formatDateTime(DateUtil.date(i))).orElse(StrUtil.EMPTY);
        this.setTraineeTime(traineeTime);

        String pUserLevel = Optional.ofNullable(UserLevelEnum.getByValue(response.getPuserLevel()))
                .map(UserLevelEnum::getDesc).orElse(StrUtil.EMPTY);
        this.setPuserLevel(pUserLevel);

        this.setPidSpecialInvite(YesOrNoEnum.getByValue(response.getPidSpecialInvite()));
        this.setSuperiorArea(areaMap.getOrDefault(response.getSuperiorAreaId(), StrUtil.EMPTY));

        if (Objects.equals(response.getPidSpecialInvite(), YesOrNoEnum.YES.getValue())) {
            String superiorSpecialInviteTime = Optional.ofNullable(response.getSuperiorSpecialInviteTime())
                    .map(i -> DateUtil.formatDateTime(DateUtil.date(i))).orElse(StrUtil.EMPTY);
            this.setSuperiorSpecialInviteTime(superiorSpecialInviteTime);
        } else {
            this.setSuperiorSpecialInviteTime(StrUtil.DASHED);
        }

    }

}
